package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.sun.org.apache.regexp.internal.RE;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

//123
    @RequestMapping("/add")
//    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
    public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        System.out.println(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            System.out.println(checkitemId);
        }
        try {
            checkGroupService.add(checkGroup, checkitemIds);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = checkGroupService.findPage(queryPageBean);
        return page;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
            CheckGroup checkGroup = checkGroupService.findById(id);
            if(checkGroup!=null){
                Result result = new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS);
                result.setData(checkGroup);
                return result;
            }
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }


    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        try {
            List<Integer> checkItemIdsByCheckGroupId = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(checkItemIdsByCheckGroupId);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup formData) {
        try {
            checkGroupService.edit(checkitemIds, formData);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckGroup>list=checkGroupService.findAll();
           return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkGroupService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
}
